package com.cakeclient.Model;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.cakeclient.database.DataSource;
import com.cakeclient.database.IDatabasable;
import com.cakeclient.database.ProductTable;

import java.util.ArrayList;
import java.util.List;

public class Product implements IDatabasable {
    private Long idDB;
    private Long remoteID;
    private String commentary;
    private ProductType type;
    private ProductStatus status = ProductStatus.COMMANDE;
    private Store storage;

    public Product(String name, Long idDB) {
        this.idDB = idDB;
        this.type = new ProductType(name);
    }


    public Product(String name, Long idDB, int illustration, String description) {
        this.idDB = idDB;
        this.type = new ProductType(name, illustration, description);
    }

    private Product(DataSource dataSource, Cursor cursor) {
        idDB = cursor.getLong(cursor.getColumnIndexOrThrow(ProductTable.ID));
        setRemoteID(cursor.getLong(cursor.getColumnIndexOrThrow(ProductTable.REMOTE_ID)));
        commentary = cursor.getString(cursor.getColumnIndexOrThrow(ProductTable.COMMENTS));
        type = ProductType.getOne(dataSource, cursor.getLong(cursor.getColumnIndexOrThrow(ProductTable.ID_PRODUCTTYPE)));
        status = ProductStatus.get(cursor.getInt(cursor.getColumnIndexOrThrow(ProductTable.ID_STATUS)));
        storage = Store.getOne(dataSource, cursor.getLong(cursor.getColumnIndexOrThrow(ProductTable.ID_STORAGE)));

    }


    public Long getRemoteID() {
        return remoteID;
    }

    private void setRemoteID(Long remoteID) {
        this.remoteID = remoteID;
    }

    public Product(ProductType productType) {
        this.type = productType;
    }

    public ProductStatus getStatus() {
        return status;
    }

    public void setStatus(ProductStatus status) {
        this.status = status;
    }

    public String getCommentary() {
        return commentary;
    }

    public void setCommentary(String commentary) {
        this.commentary = commentary;
    }

    public ProductType getType() {
        return type;
    }

    public void setType(ProductType type) {
        this.type = type;
    }

    public Long getIdDB() {
        return idDB;
    }

    public void setIdDB(Long idDB) {
        this.idDB = idDB;
    }

    public String getTypeName() {
        return this.type.getName();
    }

    public void setTypeName(String name) {
        this.type.setName(name);
    }

    public int getIllustration() {
        return this.type.getIdIllustration();
    }

    public void setIdIllustration(int idIllustration) {
        this.type.setIdIllustration(idIllustration);
    }

    public String getDescription() {
        return this.type.getDescription();
    }

    public void setDescription(String description) {
        this.type.setDescription(description);
    }

    public Store getStorage() {
        return storage;
    }

    public void setStorage(Store storage) {
        this.storage = storage;
    }

    /**
     * Depreciated
     * private void setIDPicture(Integer idPicture) {
     * switch (idPicture) {
     * case 0:
     * this.setIdIllustration(R.drawable.cakeflowerpower);
     * break;
     * case 1:
     * this.setIdIllustration(R.drawable.chococoulant);
     * break;
     * case 2:
     * this.setIdIllustration(R.drawable.defaut);
     * break;
     * case 3:
     * this.setIdIllustration(R.drawable.gatobonbon);
     * break;
     * case 4:
     * this.setIdIllustration(R.drawable.gatokipik);
     * break;
     * case 5:
     * this.setIdIllustration(R.drawable.gatomagik);
     * break;
     * case 6:
     * this.setIdIllustration(R.drawable.gatovanille);
     * break;
     * case 7:
     * this.setIdIllustration(R.drawable.heinecake);
     * break;
     * case 8:
     * this.setIdIllustration(R.drawable.meatcake);
     * break;
     * case 9:
     * this.setIdIllustration(R.drawable.niancake);
     * break;
     * case 10:
     * this.setIdIllustration(R.drawable.poetrycake);
     * break;
     * case 11:
     * this.setIdIllustration(R.drawable.tastycake);
     * break;
     * case 12:
     * this.setIdIllustration(R.drawable.wiicake);
     * break;
     * default:
     * <p/>
     * this.setIdIllustration(R.drawable.defaut);
     * <p/>
     * }
     * }
     */


    @Override
    public ContentValues getValues() {
        ContentValues values = new ContentValues();
        values.put(ProductTable.REMOTE_ID, this.getRemoteID());
        values.put(ProductTable.COMMENTS, this.commentary);
        values.put(ProductTable.ID_PRODUCTTYPE, this.getType() != null ? this.getType().getIdDB() : null);
        values.put(ProductTable.ID_STORAGE, this.getStorage() != null ? this.getStorage().getIdDB() : null);
        values.put(ProductTable.ID_STATUS, this.status.getAvancement());
        return values;
    }


    @Override
    public void saveToDatabase(Context context) {
        //Protection anti-doublons
        Product bis = null;
        if (this.getIdDB() == null && this.getRemoteID() != null && (bis = Product.getOneFromRemoteID(context, this.getRemoteID())) != null) {
            this.setIdDB(bis.getIdDB());
        }

        DataSource dataSource = new DataSource(context);
        dataSource.open();
        if (this.getIdDB() == null) {
            dataSource.insert(this, ProductTable.getInstance());
        } else {
            dataSource.update(this, ProductTable.getInstance());
        }
        dataSource.close();
    }

    @Override
    public void removeFromDatabase(Context context) {
        if (this.getIdDB() != null) {
            DataSource dataSource = new DataSource(context);
            dataSource.open();
            dataSource.delete(this, ProductTable.getInstance());
            dataSource.close();
        }
    }

    public static Product getOneFromRemoteID(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOneFromRemoteID(idDB, ProductTable.getInstance());
        cursor.moveToFirst();
        Product item = null;
        while (!cursor.isAfterLast()) {
            item = new Product(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static Product getOne(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOne(idDB, ProductTable.getInstance());
        cursor.moveToFirst();
        Product item = null;
        while (!cursor.isAfterLast()) {
            item = new Product(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static List<Product> getAll(Context context) {
        List<Product> result = new ArrayList<Product>();
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchAll(ProductTable.getInstance());
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            result.add(new Product(dataSource, cursor));
            cursor.moveToNext();
        }
        dataSource.close();
        return result;
    }

    public static Product getOne(DataSource dataSource, long idDB) {
        Cursor cursor = dataSource.fetchOne(idDB, ProductTable.getInstance());
        cursor.moveToFirst();
        Product item = null;
        while (!cursor.isAfterLast()) {
            item = new Product(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        return item;
    }
}
